<?php


namespace App\backend\services;


use App\backend\models\AdminRoleUsersModel;
use \App\backend\models\AdminUsersModel;
use Illuminate\Support\Facades\Hash;

class AdminUsersServices
{

    /**
     * 获取 - 操作员列表
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function getList($data)
    {

        $page = !isset($data['page']) ? 1 : $data['page'];
        unset($data['page']);
        $limit = !isset($data['limit']) ? 10 : $data['limit'];
        unset($data['limit']);
        $list = AdminUsersModel::listed($page,$limit,$data);

        return $list;
    }

    /**
     * 获取 - 操作员详情
     *
     * @param $data
     * @return array
     */
    public static function getDetail($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }

        $data['id'] = !empty($data['id']) ? $data['id'] : 0;
        $data['is_deleted'] = 0;

        $list = AdminUsersModel::detail($data);

        return $list;
    }

    /**
     * 添加/编辑 - 操作员
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function modify($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }

        $id = !empty($data['id']) ? $data['id'] : 0;
        unset($data['id']);

        $roleId = !empty($data['role_id']) ? $data['role_id'] : 0;
        unset($data['role_id']);

        if ($id) {
            $data['updated_at'] = time();
            $list = AdminUsersModel::updatedData($id, $data);
            //修改操作员角色关系表
            $updateArr = ['role_id' => $roleId, 'updated_at' => time()];
            AdminRoleUsersModel::updatedData($id,$updateArr);
        } else {
            $data['status'] = 1;
            $data['type'] = 3;
            $data['password'] = Hash::make($data['password']);
            unset($data['confirm_password']);
            $data['created_at'] = time();
            $insert_id = AdminUsersModel::insertData($data);
            //添加修改操作员角色关系表
            $addArr = [
                'user_id' => $insert_id,
                'role_id' => $roleId,
                'created_at' => time()
            ];
            $list = AdminRoleUsersModel::insertData($addArr);
        }

        return $list;
    }

    /**
     * 改变审核状态
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function updateStatus($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }

        $data['id'] = !empty($data['id']) ? $data['id'] : 0;
        $update_data['status'] = !empty($data['status']) ? $data['status'] : 1;
        unset($data['status']);
        $update_data['updated_at'] = time();

        $list = AdminUsersModel::deleteds($data, $update_data);
        return $list;
    }

    /**
     * 更新数据
     *
     * @param $login_ip //登陆IP
     * @param $user_id //用户ID
     * @param $token //用户token
     * @param $login_time //登陆时间
     * @return array
     */
    public static function updateLoginData($login_ip, $user_id, $token, $login_time)
    {
        $data = [
            'lastip' => $login_ip,
            'lastvisit_time' => $login_time,
            'remember_token' => $token
        ];
        $list = AdminUsersModel::updatedData($user_id, $data);
        return $list;
    }

    /**
     * 删除 - 操作员
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function delete($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }

        $data['id'] = !empty($data['id']) ? $data['id'] : 0;
        $update_data['is_deleted'] = !empty($data['is_deleted']) ? $data['is_deleted'] : 1;
        unset($data['is_deleted']);
        $update_data['deleted_at'] = time();

        $list = AdminUsersModel::deleteds($data, $update_data);
        return $list;
    }
}
